
package com.probiz.estore.content.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.probiz.estore.common.model.content.Content;
import com.probiz.estore.common.model.product.ProductStat;
import com.probiz.estore.content.model.ContentConstants;
import com.probiz.estore.content.service.ContentManager;
import com.probiz.estore.content.service.ContentNtManager;
import com.probiz.estore.core.search.SearchCriteria;

public class ContentNtManagerImpl implements ContentNtManager {
	protected final Log logger = LogFactory.getLog(getClass());
	
	private ContentManager	contentManager;

	
	public void setContentManager(ContentManager contentManager) {
		this.contentManager = contentManager;
	}

	 /*
	  * (non-Javadoc)
	  * @see com.probiz.estore.content.service.ContentNtManager#recordViewCount(java.lang.Integer, javax.servlet.http.HttpSession)
	  * 原理:把客户查看过的所有文章的id作为session的一个值L（List<Integer>）保存起来，如果当前查看的文章id被包含在L中则忽略，否则把id记录在L中，同时增加文章点击率
	  */
    public void recordViewCount(Integer contentId,HttpSession session){
    	List<Integer> viewContentIdList = (List<Integer>)session.getAttribute(ContentConstants.VIEW_CONTENT_ID_LIST);
    	if(null == viewContentIdList){
    		viewContentIdList = new ArrayList<Integer>();
    	}
    	if(!viewContentIdList.contains(contentId)){
    		viewContentIdList.add(contentId);
    		//使用原生sql，否则前台页面会有高并发问题
    		try{
    			Content  content = contentManager.getById(contentId);
    			contentManager.updateStatField(content.getContentStat().getContentStatId(),"viewCount",1);
    		}catch(Exception e){
    			logger.error(this, e);
    		}
    		session.setAttribute(ContentConstants.VIEW_CONTENT_ID_LIST, viewContentIdList);
    	}
    }
	/*
	 * (non-Javadoc)
	 * @see com.probiz.estore.content.service.ContentNtManager#updateContentStautsForScheduling()
	 */
	public void updateContentStautsForScheduling() {
		updateExpiredContents();
		updateToPulishedContents();

	}

	private void updateExpiredContents() {
		Date now = new Date();
        StringBuffer sb = new StringBuffer();
        sb.append("from Content c where c.status =1 and ");
        sb.append("(c.expiredTime is not null and c.expiredTime < ? )");
        Object[] paramValues = new Object[]{now};
		String hql = sb.toString();
		boolean flag = true;
		int pageSize = 10;
		int pageNo = 1;
		do {
			SearchCriteria sc = SearchCriteria.getHqlPagingInstance(hql, paramValues, pageNo, pageSize, null);
			List<Content> contentList = contentManager.searchByCriteria(sc);
			for (Content content : contentList) {
				try{
					content.setStatus(Content.STATUS_INACTIVE);
					contentManager.save(content);
					contentManager.flush();
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			if (contentList.size() != pageSize) {
				flag = false;
			} else {
				++pageNo;
			}
		} while (flag);
	}
	
	private void updateToPulishedContents() {
		Date now = new Date();
		StringBuffer sb = new StringBuffer();
        sb.append("from Content c where c.status <> 1 and c.isShow = 1 and ");
        sb.append(" c.publishTime < ? and ");
        sb.append("(c.expiredTime is  null or c.expiredTime > ? )");
        Object[] paramValues = new Object[]{now, now};
		String hql = sb.toString();
		boolean flag = true;
		int pageSize = 10;
		int pageNo = 1;
		do {
			SearchCriteria sc = SearchCriteria.getHqlPagingInstance(hql, paramValues, pageNo, pageSize, null);
			List<Content> contentList = contentManager.searchByCriteria(sc);
			for (Content content : contentList) {
				try{
					content.setStatus(Content.STATUS_PUBLISHED);
					contentManager.save(content);
					contentManager.flush();
				}catch(Exception e){
					e.printStackTrace();
				}
			}
			if (contentList.size() != pageSize) {
				flag = false;
			} else {
				++pageNo;
			}
		} while (flag);
	}

}
